Imports Clo = Enhancements.Core.Logging
Imports Fun = Enhancements.Basic.Expressions.Functions
Imports Gen = System.Collections.Generic
Imports Glo = System.Globalization
Imports Lev = Enhancements.Basic.Expressions.Levels
Imports Ope = Enhancements.Basic.Expressions.Operators
Imports Ref = System.Reflection
Imports Sys = System
Imports Val = Enhancements.Basic.Expressions.Values

Namespace Enhancements.Basic.Expressions
    Partial Class Compiler
        <Serializable(), DebuggerStepThrough()> _
            Private NotInheritable Class __ExpressionVariablesTypesRequest

            Private Sub New()
            End Sub

            ''' <exception cref="IsNullException"> </exception>
            ''' <exception cref="IsListFilledNotException"> </exception>
            ''' <exception cref="CompilerExpressionVariablesTypesRequestAbortedException"> </exception>
            ''' <exception cref="CompilerExpressionVariableTypeIsValidNotException"> </exception>
            Friend Shared Function RequestAndAssertAndDefine(ByVal _compiler As Compiler, ByVal _expression As System.String, ByVal _tokens As Gen.List(Of Token)) As Variable()
                _compiler.IsNullNot("_compiler", True)
                _tokens.IsListFilled("_tokens", True)

                Dim _returnValue() As Variable = Variable.___GetDistinct(_tokens)
                If _returnValue.Length.IsInRange("_returnValue.Length", 1, Sys.Int32.MaxValue, False) Then
                    Dim _eventArgs As CompilerExpressionVariablesTypesRequestEventArgs = New CompilerExpressionVariablesTypesRequestEventArgs(_returnValue)
                    Try
                        _compiler.___OnExpressionVariablesTypesRequest(_compiler, _eventArgs)
                    Catch _exception As Exception
                        CompilerExpressionVariablesTypesRequestAbortedException.___Throw(_exception)
                    End Try
                    For Each _eventArgsVariable As CompilerExpressionVariablesTypesRequestEventArgsVariable In _eventArgs.Variables
                        For _counter As Sys.Int32 = 0 To _tokens.Count - 1
                            If _tokens(_counter).IsOfType("_tokens(_counter)", GetType(Variable), False) Then
                                Dim _tokenVariable As Variable = CType(_tokens(_counter), Variable)
                                If Variable.___NamesReferToTheSameVariable(_eventArgsVariable.Name, _tokenVariable.ExpressionPart) Then
                                    Try
                                        _tokenVariable.Type = _eventArgsVariable.Type
                                    Catch _exception As Exception
                                        CompilerExpressionVariableTypeIsValidNotException.___Throw(_expression, _tokenVariable, _eventArgsVariable.Type, _exception)
                                    End Try
                                End If
                            End If
                        Next _counter
                    Next _eventArgsVariable
                End If

                Return _returnValue
            End Function

        End Class
    End Class
End Namespace
